const Seneca = require("seneca")

module.exports = class SenecaClient {

    constructor( opt ){
        this.opt = opt 
        this.seneca = new Seneca( {
            log : {
                level : "warn+"
            } ,
            short : true 
        } )
    }

    init( ){
        const clients = opt.clients 
        //set clients 
        for( let idx in clients ){
            const client = clients[ idx ]
            this.seneca.client( client )
        }
    }

    async act( cmd ){
        const app = this.seneca 
        return new Promise( function( resolve , reject ){
            //serverName : action
            app.act( cmd , function( err , result ){
                Log.debug("Seneca请求参数:" , cmd )
                if( err ) { 
                    GetLogger("err").error( "请求失败：" , err )
                    reject( err )
                }
                Log.debug("Seneca响应结果：" , result )
                resolve( result )
            })
        } ) 
    }

}